package com.ruoyi.hr.controller.hr.trainmanage;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.hr.service.trainmanage.TrainManageService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 培训管理
 * @date 2020/4/22
 */
@Controller
@RequestMapping("/trainManage")
public class TrainManageController extends BaseController {

    @Autowired
    TrainManageService trainManageService;

    private String prefix = "hr/trainmanage";

    /**
     * 跳转主页
     */
    @GetMapping()
    public String trainManage() {
        return prefix + "/trainManage";
    }

    @RequestMapping("/findDataList")
    @ResponseBody
    public TableDataInfo findDataList(@RequestParam Map map) {
        startPage();
        List<Map<String, Object>> maps = trainManageService.findDataList(map);
        return getDataTable(maps);
    }

    @GetMapping("/addTrain")
    public String addTrain() {
        return prefix + "/addTrain";
    }

    @ResponseBody
    @RequestMapping("/add")
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(trainManageService.add(map));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("trainInfo", trainManageService.findDataById(id));
        return prefix + "/editTrain";
    }

    @ResponseBody
    @RequestMapping("/edit")
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(trainManageService.edit(map));
    }

    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(trainManageService.remove(ids));
    }

    @GetMapping("/staffing")
    public String staffing(String id, ModelMap mmap) {
        List<Map<String, Object>> findTrainEmp = trainManageService.findTrainEmp(id);
        mmap.put("TrainEmp", findTrainEmp);
        mmap.put("id", id);
        return prefix + "/staffing";
    }

    @GetMapping("/upload")
    public String upload(String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/upload";
    }

    @RequestMapping("/findEmp")
    @ResponseBody
    public TableDataInfo findEmp(@RequestParam Map map) {
        startPage();
        List<Map<String, Object>> maps = trainManageService.findEmp(map);
        return getDataTable(maps);
    }

    @RequestMapping("/addEmp")
    @ResponseBody
    public BaseResult addEmp(String trainId, String AllEmpEid) {
        trainManageService.removeTrainEmp(trainId);
        if ("".equals(AllEmpEid)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        int num = trainManageService.addEmp(trainId, AllEmpEid);
        if (num > 0) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @RequestMapping("/findFileDataList")
    @ResponseBody
    public TableDataInfo findFileDataList(@RequestParam Map map) {
        startPage();
        return getDataTable(trainManageService.findFileDataList(map));
    }

    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, @RequestParam Map map) throws IOException {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        String fileName = FileUploadUtils.upload(filePath, file);

        String arry[] = fileName.split("/");
        String deleteFileName = arry[arry.length - 1];
        String deletePath = Global.getUploadPath() + "/" + FileUploadUtils.extractFilename(file);
        String arry1[] = deletePath.split("/");
        deletePath = "";
        for (int i = 0; i < arry1.length - 1; i++) {
            deletePath += arry1[i] + "/";
        }
        deletePath += deleteFileName;
        map.put("deletePath", deletePath);
        fileName = fileName.substring(1);
        map.put("fileName", fileName);
        return toAjax(trainManageService.addFile(map));
    }

    @RequestMapping("/removeFile")
    @ResponseBody
    public AjaxResult removeFile(String ids) {
        return toAjax(trainManageService.removeFile(ids));
    }

    @GetMapping("/getInstructorName")
    public String getInstructorName() {
        return prefix + "/getInstructorName";
    }


    @GetMapping("/toEmpList")
    public String toEmpList(String id, ModelMap mmap) {
        mmap.put("trainId", id);
        return prefix + "/toEmpList";
    }

    @RequestMapping("/findEmpList")
    @ResponseBody
    public TableDataInfo findEmpList(String trainId) {
        startPage();
        return getDataTable(trainManageService.findEmpList(trainId));
    }

    /**
     * 导出
     */
    @RequestMapping("/exportData")
    @ResponseBody
    public AjaxResult exportSalaryData(String trainId) throws Exception {
        return trainManageService.exportData(trainId);
    }


    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, String trainId) throws Exception {
        return trainManageService.importData(file, trainId);
    }

    @RequestMapping("isSeal")
    @ResponseBody
    public BaseResult isSeal(String trainId) {
        return trainManageService.isSeal(trainId);
    }

    @GetMapping("/history")
    public String history(){
        return prefix + "/history/trainHistory";
    }

    @RequestMapping("/findDataListHistory")
    @ResponseBody
    public TableDataInfo findDataListHistory(@RequestParam Map map) {
        startPage();
        List<Map<String, Object>> maps = trainManageService.findDataListHistory(map);
        return getDataTable(maps);
    }

    @GetMapping("/uploadHistory")
    public String uploadHistory(String id, ModelMap mmap){
        mmap.put("id", id);
        return prefix + "/history/uploadHistory";
    }

    @GetMapping("/toEmpListHistory")
    public String toEmpListHistory(String id, ModelMap mmap) {
        mmap.put("trainId", id);
        return prefix + "/history/toEmpListHistory";
    }


}


